var map; 
var geocoder;

function loadMap() {
     if (GBrowserIsCompatible()) {
         map = new GMap2(document.getElementById("map"),{size: new GSize(350,300)});
	 map.setMapType(G_HYBRID_MAP);
         map.addControl(new GSmallMapControl());
         map.addControl(new GMapTypeControl());

         var center = new GLatLng(47.635621, 6.859438); // Belfort ENM
         map.setCenter(center, 17);
	 map.enableScrollWheelZoom();
	 
         geocoder = new GClientGeocoder();
	 
         var marker = new GMarker(center, {draggable: true});  
         map.addOverlay(marker);
     }
 }

function showPosition(lat, long) {
        var dist = document.getElementById("distance");

        var center = new GLatLng(lat, long);

	document.getElementById("id_map_lat").value = center.lat().toFixed(7);
	document.getElementById("id_map_lon").value = center.lng().toFixed(7);
	document.getElementById("text_long").innerHTML = center.lng().toFixed(7);
	document.getElementById("text_lat").innerHTML = center.lat().toFixed(7);
	map.clearOverlays();
	map.setCenter(center, 17);
	var marker = new GMarker(center, {draggable: true});  
	map.addOverlay(marker);
	    
	GEvent.addListener(marker, "dragend", function() {
	    var pt = marker.getPoint();
	    map.panTo(pt);
	    document.getElementById("id_map_lat").value = pt.lat().toFixed(7);
	    document.getElementById("id_map_lon").value = pt.lng().toFixed(7);
	    document.getElementById("text_long").innerHTML = pt.lng().toFixed(7);
	    document.getElementById("text_lat").innerHTML = pt.lat().toFixed(7);
        });
		    
		    
        GEvent.addListener(map, "moveend", function() {
            map.clearOverlays();
	    var center = map.getCenter();
	    var marker = new GMarker(center, {draggable: true});
	    map.addOverlay(marker);
	    document.getElementById("id_map_lat").value = center.lat().toFixed(7);
	    document.getElementById("id_map_lon").value = center.lng().toFixed(7);
	    document.getElementById("text_long").innerHTML = center.lng().toFixed(7);
   	    document.getElementById("text_lat").innerHTML = center.lat().toFixed(7);

    	    GEvent.addListener(marker, "dragend", function() {
    	        var pt = marker.getPoint();
    		map.panTo(pt);
    		document.getElementById("id_map_lat").value = pt.lat().toFixed(7);
    		document.getElementById("id_map_lon").value = pt.lng().toFixed(7);
    		document.getElementById("text_long").innerHTML = pt.lng().toFixed(7);
    		document.getElementById("text_lat").innerHTML = pt.lat().toFixed(7);
                
   	    });
	});
}
